<?php

/**
 * 优惠券批次号
 *
 */
class Application_Model_CouponBatch extends Application_Model_DomainObject {
    
    protected $_id;
    
    protected $_name;
    /**
     * 批次号
     */
    protected $_no;
    /**
     * 创建时间
     */
    protected $_dateCreated;
    /**
     * 开始时间
     */
    protected $_dateStart;
    /**
     * 结束时间
     */
    protected $_dateEnd;
    
    /**
     * 类型
     */
    protected $_type;
    
    /**
     * 需要达到的金额,0为不限
     */
    protected $_money;

    /**
     *折扣金额
     * @var type 
     */
    protected $_discount;
    
    /**
     * 用户等级
     */
    protected $_userType;
    
    /**
     * 此批次最大发出优惠券数量
     */
    protected $_maxCount;

    /**
     * 已经发出优惠券数
     */
    protected $_giveCount;
    /**
     * 有效时期
     */
    protected $_validNum;
    /**
     * 使用范围
     */
    protected $_scope;
    /**
     * 状态
     */
    protected $_status;

    /**
     * 根据批次号返回优惠券批次
     * @param type $no 批次号
     * @return Application_Model_CouponBatch 
     */
    public static function getByNo($no){
        return Application_Model_CouponBatch::getDao()->queryOne(array('no'=>$no));
    }
    
    /**
     * 指定用户是否能领取优惠券
     * @param type $userId 用户ID
     * @param array $message 错误信息
     * @return boolean 
     */
    public function IsVaild($userId,&$message){
       $user = Application_Model_User::load($userId);
       if($user->State == Application_Model_User::STATUS_New){
           $message = '对不起，您需要验证您的邮箱才能领取优惠券';
           return FALSE;
       }
       
       $array = Application_Model_Coupon::search($userId,$this->_no);
       if(count($array)>0){
           $message = '对不起，您已经领过此优惠券了，不能重复领用';
           return FALSE;
       }
       
       if($this->_dateEnd < date('Y-m-d')){
           $message = '对不起，本次活动已经结束了';
           return FALSE;
       }
       
       if($this->_giveCount >= $this->_maxCount){
           $message = '对不起，本次优惠券已经领完了，如有疑问请联系客服';
           return FALSE;
       }
       
       return TRUE;
    }

    /**
     * 根据批次信息创建一张优惠券
     * @return Application_Model_Coupon 
     */
    public function createCoupon($userId = NULL){
        $coupon = new Application_Model_Coupon();
        $coupon->DateCreated = date('Y-m-d H:i:s');
        $coupon->Type = $this->_type;
        $coupon->Money = $this->_money;
        $coupon->Discount = $this->_discount;
        $coupon->StartTime = $this->_dateStart;
        $coupon->DateExpired = $this->_dateEnd;
        $coupon->Status = Application_Model_Coupon::STATUS_OK;
        $coupon->UserType = $this->_userType;
        $coupon->Name = $this->_name;
        $coupon->BatchNo = $this->_no;
        $coupon->UserId = $userId;
        $coupon->UseType = Application_Model_Coupon::useType_Default;
        $coupon->No = $this->_no . time();
        return $coupon;
    }
    
    public static function batchall()
    {
        $where[' dateEnd > '] = date("Y-m-d H:i:s");
        return Application_Model_CouponBatch::getDao()->queryAll($where);
    }
    
    public static function getBatchAll()
    {
        return Application_Model_CouponBatch::getDao()->queryAll();
    }
    
     public static function getByFilter($quick, $timescope, $starttime, $endtime, $selType, $scope, $pagesize, $pageindex)
    {
        $where = array();
        $and = "";
        if($quick != NUll){
            $where[$and.' no = '] = $quick;
            $and = " and ";
        }
        if($timescope == "dateCreated") {
            if($starttime != NUll){
                $where[$and.' dateCreated >= '] = $starttime." 00:00:00";
                $and = " and ";
            }
            if($endtime != NUll){
                $where[$and.' dateCreated <= '] = $endtime." 23:59:59";
                $and = " and ";
            }
        } else if($timescope == "startTime") {
            if($starttime != NUll){
                $where[$and.' dateStart >= '] = $starttime." 00:00:00";
                $and = " and ";
            }
            if($endtime != NUll){
                $where[$and.' dateStart <= '] = $endtime." 23:59:59";
                $and = " and ";
            }
        } else if($timescope == "dateExpired") {
            if($starttime != NUll){
                $where[$and.' dateEnd >= '] = $starttime." 00:00:00";
                $and = " and ";
            }
            if($endtime != NUll){
                $where[$and.' dateEnd <= '] = $endtime." 23:59:59";
                $and = " and ";
            }
        }
        if($selType != NUll){
            $where[$and.' type = '] = $selType;
            $and = " and ";
        }
        if($scope != NUll){
            $where[$and.' scope = '] = $scope;
            $and = " and ";
        }
        return Application_Model_CouponBatch::getDao()->queryPage($where, 'id desc ', $pagesize, $pageindex);
    }
    public static function getisno($no)
    {
        $where[' no = '] = $no;
        return Application_Model_CouponBatch::getDao()->queryOne($where);
    }
    public static function addcouponbatch($no,$arrup)
    {
        $batchItem = Application_Model_CouponBatch::getisno($no);
        if($batchItem){
            return 22;
        }else{
            $batchObj = new Application_Model_CouponBatch();
            foreach ($arrup as $key => $value) {
                $batchObj->$key = $value;
            }
            $batchObj->giveCount = 0;
            $batchObj->status = 1;
            $batchObj->save();
            Application_Model_Operate::addOperate(0, null, $batchObj->no, "添加优惠券批次".$batchObj->no, 0);
            return 1;
        }
    }
    
    public function getTypeName() {
        switch ($this->_type) {
            case Application_Model_Coupon::Type_SatisfyReduce:
                return "满减型抵用券";
            case Application_Model_Coupon::Type_All:
                return "通用型抵用券";
            case Application_Model_Coupon::Type_Abatement:
                return "首重减免券";
            default :
                return "通用型抵用券";
        }
    }
    
    public function getScopeName() {
        $arrScope = Application_Model_Coupon::getbyscope();
        return $arrScope[$this->_scope];
    }
}
